package com.frame.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.excel.EasyExcel;
import com.frame.config.MinioUpload2;
import com.frame.config.PageBean;
import com.frame.config.Result;
import com.frame.config.UploadMinioUtils;
import com.frame.domain.City;
import com.frame.domain.Crop;
import com.frame.domain.SysFactory;
import com.frame.service.CityService;
import com.frame.service.CropService;
import com.frame.service.SysFactoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("SysFactory")
public class SysFactoryController {

    @Autowired
    SysFactoryService sysFactoryService;

    @Autowired
    CityService cityService;

    @Autowired
    CropService cropService;

    @RequestMapping("FactoryList")
    public Result FactoryList(@RequestBody SysFactory sysFactory, PageBean pageBean){
        PageHelper.startPage(pageBean);
        List<SysFactory> list = sysFactoryService.FactoryList(sysFactory);
        PageInfo<SysFactory> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @RequestMapping("FactoryAdd")
    public Result FactoryAdd(@RequestBody SysFactory sysFactory){
        sysFactory.setFactoryCreationTime(new Date());
        boolean save =sysFactoryService.FactoryAdd(sysFactory);
        return save?Result.success("添加成功"):Result.fail("添加失败");
    }

    @RequestMapping("FactoryDelete")
    public Result FactoryDelete(Integer factoryId){
        boolean b =sysFactoryService.FactoryDelete(factoryId);
        return b?Result.success("删除成功"):Result.fail("删除失败");
    }

    @RequestMapping("FactoryUpdate")
    public Result FactoryUpdate(@RequestBody SysFactory sysFactory){
        boolean update = sysFactoryService.FactoryUpdate(sysFactory);
        return update?Result.success("修改成功"):Result.fail("修改失败");
    }

    @RequestMapping("/gettype")
    public Result gettype(){

        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        List<City> nodeList = cityService.getlist();

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<Object>> treeNodes = TreeUtil.<City, Object>build(nodeList, 1, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getCityId());
                    tree.setParentId(treeNode.getPid());
                    tree.setName(treeNode.getCityname());
                });

        return Result.success(treeNodes);
    }

    @RequestMapping("exFactory")
    public void exFactoryProductExcel(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("工厂信息", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        List<SysFactory> list = sysFactoryService.list();
        EasyExcel.write(response.getOutputStream(), SysFactory.class).sheet("工厂信息").doWrite(list);
    }

    @RequestMapping("upload")
    public String upload(MultipartFile file){
        String s = MinioUpload2.uploadPic(file);
        return s;
    }

    @RequestMapping("cropList")
    public Result cropList(){
        List<Crop> cropList = cropService.cropList();
        return Result.success(cropList);
    }

    @RequestMapping("import")
    public HashMap<String,Object> getImport(MultipartFile file){
        HashMap<String, Object> hashMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(), SysFactory.class,new CellDataDemoHeadDataListenerFactory(sysFactoryService)).sheet().doRead();
            hashMap.put("msg","成功");
        } catch (IOException e) {
            e.printStackTrace();
            hashMap.put("msg","失败");
        }
        return hashMap;
    }

    @RequestMapping("selectFactoryCount")
    public Result selectFactoryCount(){
        List<Map<String,Object>> mapList = sysFactoryService.selectFactoryCount();
        return Result.success(mapList);
    }

}